package com.huishu.commons.utlis;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.huishu.commons.exception.BaseException;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName：TXCloudSmsUtil
 * @Author: ljj
 * @Date: 2023/4/28 10:17
 */
@Slf4j
public class TXCloudSmsUtil {

    /**
     * 发送短信
     *
     * @param phone
     * @param jsonObject
     * @return templateCode //模版id,见《创建短信签名和模版》小节
     * @throws ClientException
     */
    public static boolean sendSms(String phone, String[] templateParam, String templateCode) {
        // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey，见《创建secretId和secretKey》小节
        Credential cred = new Credential("AKIDrbCD2c14uXwA5rX7awAHF7qSuEPTBlV9",
                "65xhJoB2kKn9MTSK4hEZ4OY4SffRXVHp");

        // 实例化要请求产品(以cvm为例)的client对象
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setSignMethod(ClientProfile.SIGN_TC3_256);
        SmsClient smsClient = new SmsClient(cred, "ap-chongqing");//第二个ap-chongqing 填产品所在的区
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setSmsSdkAppid("1400812984");//appId ,见《创建应用》小节
        String[] phones = {"+86" + phone};  //发送短信的目标手机号，可填多个。
        sendSmsRequest.setPhoneNumberSet(phones);
        sendSmsRequest.setTemplateID(templateCode);
        // String[] templateParam = {"123456", "5"};//模版参数，从前往后对应的是模版的{1}、{2}等,见《创建短信签名和模版》小节
        sendSmsRequest.setTemplateParamSet(templateParam);
        sendSmsRequest.setSign("伊丽莎白雅顿官方商城"); //签名内容，不是填签名id,见《创建短信签名和模版》小节
        try {
            SendSmsResponse sendSmsResponse = smsClient.SendSms(sendSmsRequest); //发送短信
            log.info(sendSmsResponse.toString());
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            log.error("短信发送失败:{}", e.getMessage());
            throw new BaseException("短信发送失败:" + e.getMessage());
        }
        return true;
    }
}
